package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.CourseOrder;
import com.biomatch.domain.DancePosition;
import com.biomatch.dto.Pagination;
import com.biomatch.dto.TeacherCoursePagination;
import com.biomatch.mapper.CourseOrderMapper;
import com.biomatch.service.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseOrderServiceImpl implements CourseOrderService {

    @Autowired
    CourseOrderMapper courseOrderMapper;



    @Override
    public IPage<CourseOrder> getAllByPage(Pagination pagination) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(pagination.getCurrent()-1) * pagination.getPageSize());
        params.put("pageSize",pagination.getPageSize());
        params.put("keywords",pagination.getKeywords());
        //获取总记录数据
        pagination.setTotal(courseOrderMapper.findCountByMap(params));
        IPage<CourseOrder> iPage = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());
        iPage.setRecords(courseOrderMapper.selectCourseOrderList(params));
        return iPage;
    }

    @Override
    public List<CourseOrder> getAll()  {
        return courseOrderMapper.selectList(null);
    }

    @Override
    public CourseOrder saveOrUpdate(CourseOrder courseOrder) {
        //增加
        if (courseOrder.getId() <= 0) {
                String code = String.valueOf(new Date().getTime());
                courseOrder.setCreatTime(new Date());
                courseOrder.setCourseId("C"+code.substring(5,code.length()));
                courseOrderMapper.insert(courseOrder);
                if(!StringUtils.isEmpty(courseOrder.getTeacherId()))
                {
                    courseOrderMapper.insertTeacherAndCourse(courseOrder.getTeacherId(),courseOrder.getCourseId());
                }
                return courseOrder;
        }
        //更新
        else {
                courseOrder.setUpdateTime(new Date());
                courseOrderMapper.updateById(courseOrder);
                return courseOrder;
        }
    }

    @Override
    public CourseOrder delete(CourseOrder courseOrder) {
        QueryWrapper<CourseOrder> courseOrderQueryWrapper = new QueryWrapper<>();
        courseOrderQueryWrapper.eq("courseId",courseOrder.getCourseId() );
        if (courseOrderMapper.selectCount(courseOrderQueryWrapper) > 0) {
            courseOrderMapper.deleteById(courseOrder.getId());
            return courseOrder;
        } else
        {
            return null;
        }
    }

    @Override
    public CourseOrder deleteCourse(String courseId) {
        CourseOrder courseOrder =new CourseOrder();
        if(!StringUtils.isEmpty(courseId))
        {
            courseOrderMapper.deleteCourseAndTeacherId(courseId);
            return  courseOrder;
        }
        else {
            return null;
        }

    }

    @Override
    public IPage<CourseOrder> getCourseByTeacherId(TeacherCoursePagination teacherCoursePagination) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(teacherCoursePagination.getCurrent()-1) * teacherCoursePagination.getPageSize());
        params.put("pageSize",teacherCoursePagination.getPageSize());
        params.put("teacherId",teacherCoursePagination.getTeacherId());
        //获取总记录数据
        teacherCoursePagination.setTotal(courseOrderMapper.findCountByTeacherId(params));
        IPage<CourseOrder> iPage = new Page<>(teacherCoursePagination.getCurrent(), teacherCoursePagination.getPageSize(),teacherCoursePagination.getTotal());
        iPage.setRecords(courseOrderMapper.getCourseByTeacherId(params));
        return iPage;
    }


}
